package com.centit.framework.ip.businessdata.dao;

import com.alibaba.fastjson.JSONArray;
import com.centit.framework.core.dao.CodeBook;
import com.centit.framework.ip.businessdata.po.InterfaceMethod;
import com.centit.framework.jdbc.dao.BaseDaoImpl;
import com.centit.framework.jdbc.dao.DatabaseOptUtils;
import com.centit.support.database.utils.PageDesc;
import com.centit.support.database.utils.QueryAndNamedParams;
import com.centit.support.database.utils.QueryUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;


/**
 * InterfaceMethodDao  Repository.
 * create by scaffold 2018-05-22
 *
 * @author guo_jh
 * T_INTERFACE_METHOD业务系统方法配置信息表
 */

@Repository
public class InterfaceMethodDao extends BaseDaoImpl<InterfaceMethod, String> {

    public static final Log log = LogFactory.getLog(InterfaceMethodDao.class);

    @Override
    public Map<String, String> getFilterField() {
        if (filterField == null) {
            filterField = new HashMap<String, String>();

            filterField.put("ywid", CodeBook.EQUAL_HQL_ID);


            filterField.put("recordid", CodeBook.EQUAL_HQL_ID);

            filterField.put("methodflag", CodeBook.EQUAL_HQL_ID);

            filterField.put("methodurl", CodeBook.EQUAL_HQL_ID);

            filterField.put("methodcommect", CodeBook.EQUAL_HQL_ID);

            filterField.put("sqlcontent", CodeBook.EQUAL_HQL_ID);

            filterField.put("startOrStop", CodeBook.EQUAL_HQL_ID);

            filterField.put("creater", CodeBook.EQUAL_HQL_ID);

            filterField.put("createTime", CodeBook.EQUAL_HQL_ID);

            filterField.put("updater", CodeBook.EQUAL_HQL_ID);

            filterField.put("updateTime", CodeBook.EQUAL_HQL_ID);

        }
        return filterField;
    }

    /**
     * 获取接口信息集合
     *
     * @param filterMap
     * @param pageDesc
     * @return
     */
    public JSONArray listObjectsAsJsonArray(Map<String, Object> filterMap, PageDesc pageDesc) {
        String sql = "select t1.ywid AS ID,t1.ywid,t1.methodflag AS NAME,t1.recordid AS parentid,'interface' AS datatype,to_char(t1.create_time,'yyyy-MM-dd hh24:mi:ss') as create_time, " +
                " t1.methodcommect,t1.start_or_stop,t1.methodurl AS url from t_interface_method t1 INNER JOIN t_interface_manager t2 ON t1.recordid = t2.recordid where t2.start_or_stop = 'T' " +
                " [ :startorstop | AND t1.start_or_stop = :startorstop ] " +
                " [ :(like)systemname | and t2.systemname like :systemname ] " +
                " order by t1.create_time desc ";
        QueryAndNamedParams params = QueryUtils.translateQuery(sql, filterMap);
        JSONArray listObjects = DatabaseOptUtils.listObjectsByNamedSqlAsJson(this, params.getQuery(), params.getParams(), null);
        return listObjects;
    }

    @Override
    public JSONArray listObjectsAsJson(Map<String, Object> filterMap, PageDesc pageDesc) {
        String sql = " SELECT T.YWID,T.RECORDID,T.METHODFLAG,T.METHODURL,T.METHODCOMMECT,T.SQLCONTENT,T.START_OR_STOP,T.CREATER,T.UPDATER, " +
                " TO_CHAR(T.CREATE_TIME, 'yyyy-MM-dd hh24:mi:ss') AS CREATE_TIME,TO_CHAR(T.UPDATE_TIME, 'yyyy-MM-dd hh24:mi:ss') AS UPDATE_TIME " +
                " FROM T_INTERFACE_METHOD T WHERE 1=1 " +
                " [ :recordid|AND T.RECORDID = :recordid ] " +
                " ORDER BY T.CREATE_TIME DESC ";
        QueryAndNamedParams params = QueryUtils.translateQuery(sql, filterMap);
        JSONArray jsonArray = DatabaseOptUtils.listObjectsByNamedSqlAsJson(this, params.getQuery(), params.getParams(), pageDesc);
        return jsonArray;
    }

    /**
     * 根据系统ID删除系统所属接口
     *
     * @param recordid 系统ID
     * @return
     */
    public boolean deleteObjectByRecordId(String recordid) {
        boolean result = false;
        String sql = " DELETE FROM T_INTERFACE_METHOD T WHERE T.RECORDID = ? ";
        try {
            int count = DatabaseOptUtils.doExecuteSql(this, sql, new Object[]{recordid});
            if (count > 0) {
                result = true;
            }
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return result;
    }
}
